package com.atguigu.queue;

import java.util.Scanner;

class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(3);
        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        char key ;
        while (flag) {
            System.out.println("请输入你的选择：1.添加数据到队列 2.获取队列数据 3.显示队列所有数据 4.查看队列头数据 5.退出");
            key = scanner.next().charAt(0);
            switch (key) {
                case '1':
                    System.out.println("请输入一个数");
                    int n = scanner.nextInt();
                    circleArrayQueue.addQueue(n);
                    break;

                case '2':
                    try {
                        int result = circleArrayQueue.getQueue();
                        System.out.println("取出的数据为：" + result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;

                case '3':
                    circleArrayQueue.showQueue();
                    break;

                case '4':
                    try {
                        circleArrayQueue.showHead();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;

                case '5':
                    scanner.close();
                    flag = false;
                    break;

                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
        System.out.println("程序已退出");
    }
}

class CircleArrayQueue {
    private final int maxSize;
    private int front;
    private int rear;
    private final int[] arr;


    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFull() {
        return (rear-front+maxSize+1)%maxSize==0;//r->2 f->0 ms=3
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        arr[rear] = n;
        rear = (rear+1)%maxSize;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
        int value = arr[front];
        front = (front+1)%maxSize;
        return value;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = front; i < front+size(); i++) {
            System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
        }
    }

    public int size(){
        return (rear-front+maxSize)%maxSize;//r->2 f->0 ms=3
    }

    public void showHead() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        System.out.println("队列头数据为：" + arr[front]);
    }
}
